Add serial port detection to Windows.
authorrobertl <robertl>
Thu, 19 Nov 2009 03:56:03 +0000 (03:56 +0000)
committerrobertl <robertl>
Thu, 19 Nov 2009 03:56:03 +0000 (03:56 +0000)
gui/serial_win.cpp

index 02a1200b4f3354bee791644341e2db911ed71263..1d564de713370eb1ee3011cd6aef82b07c009024 100644 (file)
@@ -1,4 +1,4 @@
-// $Id: serial_win.cpp,v 1.1 2009/09/02 19:05:27 robertl Exp $
+// $Id: serial_win.cpp,v 1.2 2009/11/19 03:56:03 robertl Exp $
 //------------------------------------------------------------------------
 //
 //  Copyright (C) 2009  S. Khai Mong <khai@mangrai.com>.
@@ -21,6 +21,8 @@
 
 #include "mainwindow.h"
 
+#if 0  // Does not require Windows 2000
+
 static const char *deviceNames[] = {
   "com1:",
   "com2:",
@@ -35,3 +37,35 @@ void MainWindow::osLoadDeviceNameCombos(QComboBox *box)
     box->addItem(deviceNames[i]);
   }
 }
+
+#else // This code assumes Windows 2000 or later
+
+// Uses QueryDosDevice(), Minimum supported: Windows 2000 Professional/Server
+#include <Windows.h> 
+#include <stdio.h>
+
+void MainWindow::osLoadDeviceNameCombos(QComboBox *box)
+{
+  char DevList[64*1024-1];  // a single byte more, and certain versions of windows
+                            // always return QueryDosDevice()==0 && GetLastError()==ERROR_MORE_DATA.
+                            // see http://support.microsoft.com/kb/931305
+  // Get a list of all existing MS-DOS device names. Stores one or more asciiz strings followed by an extra null.
+  DWORD res = QueryDosDeviceA(NULL, DevList, sizeof(DevList));
+  if (res == 0)
+  {
+    DWORD err = GetLastError(); // could check for ERROR_INSUFFICIENT_BUFFER, and retry with a larger buffer.
+                                // but DevList is already at the maximum size it can be without running into kb 931305.
+    // FIXME: This shold be a QMessageBox::warning() - RJL
+    // fprintf(stderr,"QueryDosDevice() failed with %d.  GetLastError()==%d.\n", res, err);
+    return;
+  }
+
+  for (char *p=DevList; *p;) {
+    int len = strlen(p);
+    if (strncmp(p,"COM",3)==0)
+      box->addItem((PCHAR)p);
+    p += len+1; // +1 to also skip the null character of each string
+  }
+}
+
+#endif